package com.sk.sourcecircle.activity.home.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jude.rollviewpager.hintview.IconHintView;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.activity.home.activity.EventDetailsActivity;
import com.sk.sourcecircle.activity.home.activity.MoreRecommendEventsActivity;
import com.sk.sourcecircle.adapter.BannerAdapter;
import com.sk.sourcecircle.adapter.EventListAdapter;
import com.sk.sourcecircle.base.BaseFragment;
import com.sk.sourcecircle.callback.HandleDataCallBack;
import com.sk.sourcecircle.callback.MyCallBack;
import com.sk.sourcecircle.entity.EventListBean;
import com.sk.sourcecircle.entity.HomeBannerBean;
import com.sk.sourcecircle.entity.HomeTabList;
import com.sk.sourcecircle.utils.AppApi;
import com.sk.sourcecircle.utils.SPUtils;
import com.sk.sourcecircle.utils.UIUtils;
import com.sk.sourcecircle.view.FixedProportionViewPager;
import com.sk.sourcecircle.view.NoticeView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by sk on 2017/11/19.
 */

public class SelectedFragment extends BaseFragment implements HandleDataCallBack, BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    FixedProportionViewPager myPager;
    HomeTabList.DataBean dataBean;
    EventListAdapter adapter;
    NoticeView noticeView;
    RelativeLayout moreLl;


    @BindView(R.id.mPtrFrameLayout)
    PtrClassicFrameLayout mPtrFrameLayout;
    private boolean isRefresh = false;
    private List<EventListBean.DataBean> dataBeanList = new ArrayList<>();

    private MyCallBack callBack;

    public MyCallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(MyCallBack callBack) {
        this.callBack = callBack;
    }

    @Override
    protected int getContentView() {
        return R.layout.fragment_selected;
    }

    public static SelectedFragment newInstance(HomeTabList.DataBean dataBean) {
        Bundle args = new Bundle();
        args.putParcelable("data", dataBean);
        SelectedFragment fragment = new SelectedFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        dataBean = getArguments().getParcelable("data");

        mPtrFrameLayout.disableWhenHorizontalMove(true);

        mPtrFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, dataRyc, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                isRefresh = true;
                doRefresh();
            }
        });

        if (SPUtils.contains(mContext, "selectEvent")) {
            String cacheEvent = (String) SPUtils.get(mContext, "selectEvent", "");
            if (cacheEvent.length() > 0) {
                EventListBean eventListBean = JSON.parseObject(cacheEvent, EventListBean.class);
                dataBeanList = eventListBean.getData();
            }
        }

        intRyc();

        noticeView.getNotice("-1");

        myPager.setHintView(new IconHintView(mContext, R.drawable.point_focus, R.drawable.point_normal, UIUtils.dip2px(mContext, 16)));

        if (SPUtils.contains(mContext, "selectBanner")) {
            String cacheBanner = (String) SPUtils.get(mContext, "selectBanner", "");
            HomeBannerBean homeBannerBean = JSON.parseObject(cacheBanner, HomeBannerBean.class);
            List<HomeBannerBean.DataBean> bannerList = homeBannerBean.getData();
            initBanner(bannerList);
        }

        if (dataBean != null) {
            AppApi.getHomeBanner(String.valueOf(dataBean.getCate_id()), null, SelectedFragment.this, 0, getActivity());
            AppApi.getRecommendEvent(String.valueOf(dataBean.getCate_id()), 3, 1, this, 2, getActivity());
        }

    }

    private void intRyc() {
        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new EventListAdapter(R.layout.item_recommend_event, dataBeanList);
        dataRyc.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        View header = LayoutInflater.from(mContext).inflate(R.layout.item_header, dataRyc, false);
        noticeView = header.findViewById(R.id.notice_view);
        myPager = header.findViewById(R.id.my_pager);
        moreLl = header.findViewById(R.id.more_ll);
        moreLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, MoreRecommendEventsActivity.class);
                intent.putExtra("id", dataBean.getCate_id());
                startActivity(intent);
            }
        });
        adapter.addHeaderView(header);
    }


    @Override
    public void handlerSuccessData(String result, int tag) {
        switch (tag) {
            case 0:
                HomeBannerBean homeBannerBean = JSON.parseObject(result, HomeBannerBean.class);
                List<HomeBannerBean.DataBean> bannerList = homeBannerBean.getData();
                initBanner(bannerList);
                SPUtils.put(mContext, "selectBanner", result);
                break;
            case 1:
                break;
            case 2:
                if (isRefresh) {
                    isRefresh = false;
                    mPtrFrameLayout.refreshComplete();
                }
                dataBeanList.clear();
                EventListBean eventListBean = JSON.parseObject(result, EventListBean.class);
                dataBeanList.addAll(eventListBean.getData());
                adapter.notifyDataSetChanged();
                SPUtils.put(mContext, "selectEvent", result);
                break;
        }

    }

    private void initBanner(List<HomeBannerBean.DataBean> bannerList) {
        BannerAdapter adapter = new BannerAdapter(bannerList, mContext);
        myPager.setAdapter(adapter);
    }

    @Override
    public void handlerFailData(String failString, int tag) {
    }

    @Override
    public void handleErrorData(String error, int tag) {
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(mContext, EventDetailsActivity.class);
        intent.putExtra("id", dataBeanList.get(position).getActivity_id());
        startActivity(intent);
    }

    public void doRefresh() {
        isRefresh = true;
        AppApi.getHomeBanner(String.valueOf(dataBean.getCate_id()), null, SelectedFragment.this, 0, getActivity());
        AppApi.getRecommendEvent(String.valueOf(dataBean.getCate_id()), 3, 1, this, 2, getActivity());
    }

}
